<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class Customer extends Model
{
    /**
     * The table associated with the model.
     *
     * @var string
     */
	protected $table = 'customers';

    protected $casts = [
        'city' => 'collection'
    ];

    /**
     * The attributes that aren't mass assignable.
     *
     * @var array
     */
	protected $guarded  = ['referer'];

    // 客户的订单
    public function order()
    {
        return $this->hasMany('App\Models\Order', 'cid');
    }

    // 合同
    public function contracts()
    {
        return $this->hasMany('App\Models\Contract', 'cid');
    }

    // 区域
    public function saler()
    {
        return $this->belongsTo('App\Models\User', 'saler_id');
    }

    // 笔记
    public function notes()
    {
        return $this->hasMany('App\Models\CustomerNote', 'cid');  
    }

    public function setNameAttribute($value)
    {
        if (!request('vip')) {
            $this->attributes['vip'] = 0;
        }
        $this->attributes['name'] = trim($value);
    }
}
